package com.hsurosy.domain.credit.model.aggregate;

import com.hsurosy.domain.award.model.valobj.TaskStateVO;
import com.hsurosy.domain.credit.event.CreditAdjustSuccessMessageEvent;
import com.hsurosy.domain.credit.model.entity.CreditAccountEntity;
import com.hsurosy.domain.credit.model.entity.CreditOrderEntity;
import com.hsurosy.domain.credit.model.entity.TaskEntity;
import com.hsurosy.domain.credit.model.valobj.TradeNameVO;
import com.hsurosy.domain.credit.model.valobj.TradeTypeVO;
import com.hsurosy.types.event.BaseEvent;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;

import java.math.BigDecimal;

/**
 * @Author Hsu琛君珩
 * @Date 2024-11-25 13:02
 * @Description 交易聚合对象
 * @Version: v1.0.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TradeAggregate {

    /** 用户ID */
    private String userId;
    /** 积分账户实体 */
    private CreditAccountEntity creditAccountEntity;
    /** 积分订单实体 */
    private CreditOrderEntity creditOrderEntity;
    /** 任务实体 */
    private TaskEntity taskEntity;

    /**
     * 创建积分账户实体对象
     *
     * @param userId 用户ID
     * @param adjustAmount 调整的积分金额
     * @return 构建完成的积分账户实体对象
     */
    public static CreditAccountEntity createCreditAccountEntity(String userId, BigDecimal adjustAmount) {
        return CreditAccountEntity.builder()
                .userId(userId)
                .adjustAmount(adjustAmount)
                .build();
    }

    /**
     * 创建积分订单实体对象
     *
     * @param userId 用户ID
     * @param tradeName 交易名称
     * @param tradeType 交易类型（如：充值、消费）
     * @param tradeAmount 交易金额
     * @param outBusinessNo 外部业务唯一标识
     * @return 构建完成的积分订单实体对象
     */
    public static CreditOrderEntity createCreditOrderEntity(String userId, TradeNameVO tradeName, TradeTypeVO tradeType, BigDecimal tradeAmount, String outBusinessNo) {
        return CreditOrderEntity.builder()
                .userId(userId)
                .orderId(RandomStringUtils.randomNumeric(12))
                .tradeName(tradeName)
                .tradeType(tradeType)
                .tradeAmount(tradeAmount)
                .outBusinessNo(outBusinessNo)
                .build();
    }

    /**
     * 创建任务实体对象
     *
     * @param userId 用户ID
     * @param topic 消息主题
     * @param messageId 消息唯一标识
     * @param message 消息内容
     * @return 构建完成的任务实体对象
     */
    public static TaskEntity createTaskEntity(String userId, String topic, String messageId, BaseEvent.EventMessage<CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage> message) {
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setUserId(userId);
        taskEntity.setTopic(topic);
        taskEntity.setMessageId(messageId);
        taskEntity.setMessage(message);
        taskEntity.setState(TaskStateVO.create);
        return taskEntity;
    }

}
